<?php
require_once 'abstractModel.php';

class CommunityModel extends AbstractModel {
	//TABLE NAMES
	const TABLE_NAME_COMMUNITY = 'tCommunity';
	const TABLE_NAME_COMMUNITY_BOARD = 'tCommunityBoard';
	const TABLE_NAME_COMMUNITY_BOARD_LIKE = 'tCommunityBoardLike';
	const TABLE_NAME_COMMUNITY_BOARD_RESPONSE ='tCommunityBoardReponse';
	const TABLE_NAME_COMMUNITY_CATEGORY ='tCommunityCategory';

	//COMMUNITY
	const COMMUNITY_INDEX = 'CIndex';
	const COMMUNITY_TITLE = 'CTitle';
	const COMMUNITY_USER_ID = 'userId';
	const COMMUNITY_CREATED_DATE = 'CCreatedDate';
	const COMMUNITY_REFRESHED_DATE = 'CRefreshedDate';
	const COMMUNITY_CONTENT = 'CContent';

	//COMMUNITY_BOARD
	const COMMUNITY_BOARD_INDEX = 'CBIndex';
	const COMMUNITY_BOARD_COMMUNITY_INDEX = 'CIndex';
	const COMMUNITY_BOARD_USER_ID = 'userId';
	const COMMUNITY_BOARD_TITLE = 'CBtitle';
	const COMMUNITY_BOARD_CREATED_DATE = 'CBCreatedDate';
	const COMMUNITY_BOARD_REFRESHED_DATE = 'CBRefreshedDate';
	const COMMUNITY_BOARD_CONTENT = 'CBContent';

	//COMMUNITY_BOARD_LIKE
	const COMMUNITY_BOARD_LIKE_INDEX = 'CBLIndex';
	const COMMUNITY_BOARD_LIKE_COMMUNITY_BOARD = 'CBIndex';
	const COMMUNITY_BOARD_LIKE_USER_ID = 'userId';

	//COMMUNITY_BOARD_RESPONSE
	const COMMUNITY_BOARD_RESPONSE_INDEX = 'CBRIndex';
	const COMMUNITY_BOARD_RESPONSE_COMMUNITY_BOARD = 'CBIndex';
	const COMMUNITY_BOARD_RESPONSE_USER_ID = 'userId';
	const COMMUNITY_BOARD_RESPONSE_CREATED_DATE = 'CBRCreatedDate';
	const COMMUNITY_BOARD_RESPONSE_REFRESHED_DATE = 'CBRRefreshedDate';
	const COMMUNITY_BOARD_RESPONSE_CONTENT = 'CBRContent';

	public function __construct(){
		parent::__construct();
		$this->targetTable = self::TABLE_NAME_COMMUNITY;
	}

	public function setTargetTable($targetTable){
		if ($targetTable==self::TABLE_NAME_COMMUNITY ||
			$targetTable==self::TABLE_NAME_COMMUNITY_BOARD ||
			$targetTable==self::TABLE_NAME_COMMUNITY_BOARD_LIKE ||
			$targetTable==self::TABLE_NAME_COMMUNITY_BOARD_RESPONSE ||
			$targetTable==self::TABLE_NAME_COMMUNITY_CATEGORY)
				$this->targetTable=$targetTable;
	}

	public function insert(){
		switch ($this->targetTable) {
			case self::TABLE_NAME_COMMUNITY:
				$userId = func_get_arg(0);
				$cTitle = func_get_arg(1);
				$cContent = func_get_arg(2);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(userId, CTitle, CContent, CRefreshedDate)
					values(?, ?, ?, NOW())");
				$sth->bindParam(1, $_userId);
				$sth->bindParam(2, $cTitle);
				$sth->bindParam(3, $cContent);
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD:
				$cIndex = func_get_arg(0);
				$userId = func_get_arg(1);
				$cbTitle = func_get_arg(2);
				$cbContent = func_get_arg(3);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(CIndex, userId, CBTitle, CBContent, CBRefreshedDate)
				values(?, ?, ?, ?, NOW())");
				$sth->bindParam(1, $cIndex);
				$sth->bindParam(2, $userId);
				$sth->bindParam(3, $cbTitle);
				$sth->bindParam(4, $cbContent);
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD_LIKE:
				$cbIndex = func_get_arg(0);
				$userId = func_get_arg(1);
				$sth = $this->db->prepare("select * from {$this->targetTable} where userId=?");
				$sth->bindParam(1, $userId);
				$result = $this->select($sth);
				if($result[0][self::COMMUNITY_BOARD_LIKE_USER_ID]!=$userId){
					$this->select($sth);
					$sth = $this->db->prepare("Insert into {$this->targetTable}(CBIndex, userId) values(?, ?)");
					$sth->bindParam(1, $cbIndex);
					$sth->bindParam(2, $userId);
				} else {
					return;
				}
			case self::TABLE_NAME_COMMUNITY_BOARD_RESPONSE:
				$cbIndex = func_get_arg(0);
				$userId = func_get_arg(1);
				$cbContent = func_get_arg(2);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(CBIndex, userId, CBRContent, CBRRefreshedDate)
				values(?, ?, ?, NOW())");
				$sth->bindParam(1, $cbIndex);
				$sth->bindParam(2, $userId);
				$sth->bindParam(3, $cbContent);
				break;
			case self::TABLE_NAME_COMMUNITY_CATEGORY:
				$userId = func_get_arg(0);
				$cIndex = func_get_arg(1);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(userId, CIndex)
				values(?, ?)");
				$sth->bindParam(1, $userId);
				$sth->bindParam(2, $cIndex);
				break;
		}

		$sth->execute();
		if ($sth->errorCode() != '00000'){
			var_dump(func_get_args());
			$info = $sth->errorInfo();
			print $info[2];
		}else{
			print "OK!";
		}

		return ;
	}

	public function update(){
		switch ($this->targetTable) {
			case self::TABLE_NAME_COMMUNITY:
				$cTitle = func_get_arg(0);
				$cContent = func_get_arg(1);
				$cIndex = func_get_arg(2);
				$sth = $this->db->prepare("Update {$this->targetTable} SET CTitle=?, CContent=?, CRefreshedDate=NOW()
					where CIndex=?");
				$sth->bindParam(1, $cTitle);
				$sth->bindParam(2, $cContent);
				$sth->bindParam(3, $cIndex);
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD:
				$cbTitle = func_get_arg(0);
				$cbContent = func_get_arg(1);
				$cbIndex = func_get_arg(2);
				$sth = $this->db->prepare("Update {$this->targetTable} SET CBtitle=?, CBContent=?, CBRefreshedDate=NOW()
					where CBIndex=?");
				$sth->bindParam(1, $cbTitle);
				$sth->bindParam(2, $cbContent);
				$sth->bindParam(3, $cbIndex);
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD_RESPONSE:
				$cbrContent = func_get_arg(0);
				$cbrIndex = func_get_arg(1);
				$sth = $this->db->prepare("Update {$this->targetTable} SET CBRContent=?, CBRRefreshedDate=NOW()
					where CBRIndex=?");
				$sth->bindParam(1, $cbrContent);
				$sth->bindParam(2, $cbrIndex);
				break;
		}
		$sth->execute();
	}
	public function delete($where){
		switch ($this->targetTable) {
			case self::TABLE_NAME_COMMUNITY:
				$tableIndex = self::COMMUNITY_INDEX;
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD:
				$tableIndex = self::COMMUNITY_BOARD_INDEX;
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD_LIKE:
				$tableIndex = self::COMMUNITY_BOARD_LIKE_INDEX;
				break;
			case self::TABLE_NAME_COMMUNITY_BOARD_RESPONSE:
				$tableIndex = self::COMMUNITY_BOARD_RESPONSE_INDEX;
				break;
		}
		$sth = $this->db->prepare("Delete from {$this->targetTable} where {$tableIndex} = ?");
		$sth->bindParam(1, $where);
		return $sth->execute();
	}
}
